<!--
  This file is a demo of the OpenSearchStore, a simple wrapper to any OpenSearch compliant
  search engine.
  
  Note, the simple proxy requires a curl-enabled PHP install
-->
<html>
<head>
	<title>Demo of OpenSearchStore</title>
	<style type="text/css">

		@import "../../../dijit/themes/tundra/tundra.css";
		@import "../../../dojo/resources/dojo.css";
		@import "../../../dijit/tests/css/dijitTests.css";
		@import "./openSearchDemo.css";
	</style>

	<script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true"></script>
	
	<script type="text/javascript">
		dojo.require("dojo.parser");
		dojo.require("dijit.form.TextBox");
		dojo.require("dijit.form.Button");
		dojo.require("dijit.form.FilteringSelect");
		dojo.require("dijit.form.CheckBox");
		dojo.require("dijit.form.NumberSpinner");
		dojo.require("dijit.Tree");
		dojo.require("dojox.data.OpenSearchStore");

		function init(){
			var fViewWidgets = [];

			//Set up an onComplete handler for OpenSearchData
			function onComplete(items, request){
				if(items.length > 0){
					var ul = dojo.byId("searchResults");
					var test;
					var li;
					for(var i=0; i<items.length; i++){
						li = dojo.doc.createElement("li");
						li.innerHTML = openSearchStore.getValue(items[i], "content");
						ul.appendChild(li);
					}
				}
				statusWidget.attr('value', "PROCESSING COMPLETE.");
			}
			//What to do if a search fails...
			function onError(error, request){
				statusWidget.attr('value', "PROCESSING ERROR.");
			}

			//Function to invoke the search of the openSearchStore
			function invokeSearch(){
				var tbody = dojo.byId("searchResults");
				while(tbody.childNodes.length){
					var node = tbody.childNodes.item(0);
					node.parentNode.removeChild(node);
				}
				var request = {
					query: {},
					onComplete: onComplete,
					onError: onError
				};
				if(searchTermsWidget){
					var searchTerms = searchTermsWidget.attr('value');
					if(searchTerms && searchTerms !== ""){
						var searchTermsArray = searchTerms.split(" ");
						searchTerms = "";
						for(var i = 0; i < searchTermsArray.length; i++){
							searchTerms = searchTerms + searchTermsArray[i];
							if(i < (searchTermsArray.length - 1)){
								searchTerms += ","
							}
						}
						request.query.searchTerms = searchTerms;
					}
				}

				if(countWidget){
					request.count = countWidget.attr('value');
				}

				if(statusWidget){
					statusWidget.attr('value', "PROCESSING REQUEST");
				}

				openSearchStore.fetch(request);
			}

			//Lastly, link up the search event.
			dojo.connect(dijit.byId("searchButton"), 'onClick', invokeSearch);
			var currentArgs = {url: 'http://intertwingly.net/search/'};
			var oldProcess = null;
			function setTransform(state){
				if(state){
					oldProcess = openSearchStore.processItem;
					switch(currentArgs.url){
						case 'http://intertwingly.net/search/':
							openSearchStore.processItem = intertwinglyTransform;
							break;
						case 'http://www.shutterpoint.com/opensearch.xml':
							openSearchStore.processItem = shutterpointTransform;
							break;
						case 'http://technorati.com/osd.xml':
							openSearchStore.processItem = technoratiTransform;
							break;
					}
				}else if(oldProcess !== null){
					openSearchStore.processItem = oldProcess;
				}
			}
			dojo.connect(dijit.byId('transformItem'), 'onChange', function(state){
				setTransform(state);
			});
			dojo.connect(dijit.byId('urlSelector'), 'onChange', function(args){
				currentArgs = dojo.fromJson(args);
				currentArgs.url = 'openSearchProxy.php?osd=true&url='+currentArgs.url;
				openSearchStore.close();
				openSearchStore = new dojox.data.OpenSearchStore(currentArgs);
				if(dijit.byId('transformItem').checked){
					setTransform(true);
				}
			});

			var intertwinglyTransform = function(item, attribute){
				function removeAll(/*NodeList*/list){
					while(list.length) {
						var node = list.item(0);
						node.parentNode.removeChild(node);
					}
				}
				var content = item.node.getElementsByTagName("content").item(0);
				// Remove all blockquote elements
				removeAll(content.getElementsByTagName("blockquote"));
				// Remove all pre-formatted elements
				removeAll(content.getElementsByTagName("pre"));
				return openSearchStore._getNodeXml(content, true);
			};

			var shutterpointTransform = function(item, attribute){
				var description = item.node.getElementsByTagName("description").item(0);
				var div = dojo.doc.createElement("div");
				div.innerHTML = description.childNodes.item(0).nodeValue;
				//Of the description children, remove the divs (to only leave the images)
				for(var i=0; i<div.childNodes.length; i++){
					var node = div.childNodes.item(i);
					if(node.tagName.toLowerCase() === "div")
						node.parentNode.removeChild(node);
				}
				return openSearchStore._getNodeXml(div, true);
			};

			var technoratiTransform = function(item, attribute){
				function removeAll(/*NodeList*/list){
					while(list.length) {
						var node = list.item(0);
						node.parentNode.removeChild(node);
					}
				}
				removeAll(item.node.getElementsByTagName("blockquote"));
				return item.innerHTML;
			};
		}
		dojo.addOnLoad(init);
	</script>
</head>

<body class="tundra">
	<h1>DEMO:  OpenSearchStore Search</h1>
	<hr />
	<h3>Description:</h3>
	<p>
		This simple demo shows how services, such as an OpenSearch compliant search service, can be wrapped by the datastore API.  In this demo, you can search public search engines through a simple OpenSearchStore by specifying a series of search terms (separated by spaces) to search on.  The results will be displayed below the search box.
	</p>
    <p>
        <b>NOTE:  This demo makes use of a simple PHP based proxy script.  The proxy script requires cURL support in PHP.  Without cURL support, the demo will throw errors.</b>
    </p>
	<label for="urlSelector">URL of OpenSearchDocument:</label>
	<select dojoType="dijit.form.FilteringSelect"
			id="urlSelector"
			name="urlSelector"
			autoComplete="true">
		<option value="{url: 'http://intertwingly.net/search/'}">http://intertwingly.net/search/</option>
		<option value="{url: 'http://www.shutterpoint.com/opensearch.xml'}">http://www.shutterpoint.com/opensearch.xml</option>
		<option value="{url: 'http://technorati.com/osd.xml', itemPath: '.hentry'}">http://technorati.com/osd.xml</option>
	</select>
	<label for="transformItem">Apply transform function?</label>
	<input dojoType="dijit.form.CheckBox"
		   type="checkbox"
		   id="transformItem"
		   name="transformItem">
	</input>
	<hr />
	<label for="status">Status:</label>
	<div dojoType="dijit.form.TextBox" maxLength="50" id="status" name="status" jsId="statusWidget" disabled="true"></div>
	<label for="searchTerms">Search For:</label>
	<div dojoType="dijit.form.TextBox" maxLength="50" id="searchTerms" name="searchTerms" jsId="searchTermsWidget" value="javascript"></div>
	<label for="count">Number of Results:</label>
	<div id="count"
		 name="count"
		 jsId="countWidget"
		 dojoType="dijit.form.NumberSpinner"
		 value="20"
		 constraints="{min:1,max:20}">
	</div>
	<div dojoType="dijit.form.Button" label="Search" id="searchButton" jsId="searchButtonWidget"></div>
	<hr/>
	<div dojoType="dojox.data.OpenSearchStore"
		 url="openSearchProxy.php?osd=true&url=http://intertwingly.net/search/"
		 jsId="openSearchStore">
	</div>
	<ul id="searchResults"></ul>

</body>
</html>